package com.cssw.fyzb.tps.api.adapay;

import com.cssw.fyzb.tps.config.adapay.AdapayConfig;
import com.huifu.adapay.model.Member;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/06 22:29
 */
@Function(description = "更新adapay结算账户", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "nickName", type = Type.String, description = "昵称", required = false),
        @Parameter(name = "sex", type = Type.String, description = "性别(1-男 2-女)", required = false),
        @Parameter(name = "mobile", type = Type.String, description = "手机号码", required = false),
        @Parameter(name = "userName", type = Type.String, description = "用户姓名", required = false),
        @Parameter(name = "idCard", type = Type.String, description = "身份证号码", required = false),
})
@Component
public class UpdateSettleAcctAdapay implements Action {

    private static final Logger logger = LoggerFactory.getLogger(UpdateSettleAcctAdapay.class);

    @Autowired
    private AdapayConfig adapayConfig;

    @Override
    public Response execute(Request request) throws Exception {
        String userId = request.getString("userId");
        String mobile = request.getString("mobile");
        String userName = request.getString("userName");
        String idCard = request.getString("idCard");
        String nickName = request.getString("nickName");
        String sex = request.getString("sex");

        try {
            Map<String, Object> memberParams = new HashMap<>(10);
            memberParams.put("app_id", adapayConfig.getAppId());
            memberParams.put("member_id", userId);
            memberParams.put("tel_no", mobile);
            memberParams.put("user_name", userName);

            memberParams.put("cert_type", "00");
            memberParams.put("cert_id", idCard);

            memberParams.put("nickname", nickName);
            if ("1".equals(sex)){
                memberParams.put("gender", "MALE");
            } else if ("2".equals(sex)){
                memberParams.put("gender", "FEMALE");
            }

            Map<String, Object> member = Member.create(memberParams);

            return Response.success().put("data", member);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("支付发起失败");
        }
    }
}
